package com.centerm.bluetooth.common.controller;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;

import com.centerm.bluetooth.common.Command;
import com.centerm.bluetooth.common.listener.IPinListener;
import com.centerm.bluetooth.core.controller.BaseController;
import com.centerm.bluetooth.core.controller.constants.Constants;
import com.centerm.bluetooth.core.controller.constants.WaitEnum;
import com.centerm.bluetooth.ibridge.BluetoothIBridgeDevice;
import com.centerm.bluetooth.model.Respond;
import com.centerm.bluetooth.receive.CMDParseReceiver;
import com.centerm.util.HexUtil;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Created by linwenhui on 2017/4/28.
 */

public class PinController extends BaseController<IPinListener> {

    private final static int INPUT_PIN = 0x01;
    private final static int READ_INPUT_PIN_RESLUT = 0x02;
    private int type;

    public PinController(@NonNull Context context) {
        super((byte) 0x04,context);
    }

    public boolean startInputPin(int timeout, String money) throws IOException {
        if (wait == WaitEnum.FREE) {
            ByteArrayOutputStream params = new ByteArrayOutputStream();
            params.write(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF});
            params.write(timeout);
            params.write(6);
            params.write(6);
            params.write(new byte[]{0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20});
            params.write(HexUtil.hexStr2Bytes(money));
            params.write(0x0);
            params.write(0x00);
            type = INPUT_PIN;
            boolean result = send(Command.INPUT_PIN, params.toByteArray());
            if (result) {
                LocalBroadcastManager.getInstance(context).registerReceiver(new InputPinResultResport(), new IntentFilter(Constants.CMD_ACTION + "0402"));
                setTimeOut(timeout);
            }
            params.close();
            return result;
        }
        return false;
    }

    public boolean readInputPinResult() throws IOException {
        type = READ_INPUT_PIN_RESLUT;
        boolean res = send(Command.READ_INPUT_PIN_RESULT, null);
        if (res)
            setTimeoutTimer();
        return res;
    }

    protected void parseReadInputPinResult(byte[] params) {
        byte[] randomBytes = new byte[8];
        System.arraycopy(params,0,randomBytes,0,8);
        byte[] pinblock = new byte[8];
        System.arraycopy(params, 9, pinblock, 0, 8);
        if (baseListener != null)
            baseListener.onGetInputPin(pinblock,randomBytes);

    }

    @Override
    public void onDataReceived(BluetoothIBridgeDevice device, Respond respond) {
        super.onDataReceived(device, respond);
        switch (type) {
            case INPUT_PIN:
                wait = WaitEnum.WAIT_INPUT_PIN;
                if (baseListener != null)
                    baseListener.onWaitForInputPin();
                break;
            case READ_INPUT_PIN_RESLUT:
                parseReadInputPinResult(respond.getContent());
                break;
        }
    }

    private class InputPinResultResport extends CMDParseReceiver {

        @Override
        public void onReceive(Context context, Intent intent, byte[] params, int paramLen) {
            wait = WaitEnum.FREE;
            if (paramLen != 0) {
                switch (params[0]) {
                    case 0x00:
                        try {
                            readInputPinResult();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        break;
                    case 0x01:

                        if (baseListener != null)
                            baseListener.onInputPinEmpty();
                        break;
                    case 0x02:
                        if (baseListener != null) {
                            baseListener.onInputPinCancel();
                        }
                        break;
                    case 0x03:
                        if (baseListener != null) {
                            baseListener.onInputPinTimeout();
                        }
                        break;
                }
            } else {
                if (baseListener != null) {
                    baseListener.onInputPinTimeout();
                }
            }
        }
    }
}
